﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Framework.Kit
{
    /// <summary>
    /// 其他处理
    /// </summary>
    public class other
    {
        /// <summary>
        /// 返回时间格式
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string getOne(int num)
        {
            if (num < 10)
            {
                return string.Format("{0:00}", num);
            }
            else
            {
                return num.ToString();
            }
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        public static void WriteErrLog(string errTitle, Exception ex)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToShortDateString()+"\\"+"\\log.txt";
            CreatDir();
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }

            StringBuilder strBuilderErrorMessage = new StringBuilder();

            strBuilderErrorMessage.Append("________________________________________________________________________________________________________________\r\n");
            strBuilderErrorMessage.Append("日期:" + System.DateTime.Now.ToString() + "\r\n");
            strBuilderErrorMessage.Append("错误标题:" + errTitle + "\r\n");
            strBuilderErrorMessage.Append("错误信息:" + ex.Message + "\r\n");
            strBuilderErrorMessage.Append("错误内容:" + ex.StackTrace + "\r\n");
            using (StreamWriter sw = File.AppendText(path))
            {
                sw.Write(strBuilderErrorMessage);
                sw.Flush();
                sw.Close();
            }
        }

        /// <summary>
        /// 判断今日的本地文件夹是否创建,没有则创建
        /// </summary>
        public static void CreatDir()
        {
            string dir = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToShortDateString();
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }
        }

        /// <summary>
        /// 判断醒目XML配置文件是否存在
        /// </summary>
        /// <param name="fileStr">文件名</param>
        /// <param name="AutoCreate">时是否自动创建</param>
        /// <param name="Root">创建时的根节点</param>
        /// <returns></returns>
        public static string CheckExists(string fileStr,bool AutoCreate,string Root)
        {
            XmlHelper xml = new XmlHelper();
            CreatDir();
            string file = AppDomain.CurrentDomain.BaseDirectory +DateTime.Now.ToShortDateString()+"\\" + fileStr;
            if (System.IO.File.Exists(file))
            {
                if (AutoCreate)
                {
                    System.IO.File.Delete(file);
                }
                xml.CreateXmlDocument(file, Root);
            }
            else
            {
                xml.CreateXmlDocument(file, Root);
            }
            return file;
        }
    }
}
